package com.comtop.springboot.templet.modules.app.controller;


import com.comtop.springboot.templet.common.utils.Ret;
import com.comtop.springboot.templet.common.validator.Assert;
import com.comtop.springboot.templet.modules.app.service.IUserService;
import com.comtop.springboot.templet.common.service.JwtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * APP登录授权
 *
 * @author wangjiafang
 * @version 1.0
 * @since 2017-03-23 15:31
 */
@RestController
@RequestMapping("/app")
public class ApiLoginController {
    /**用户服务类**/
    @Autowired
    private IUserService IUserService;
    /**jwt服务类**/
    @Autowired
    private JwtService jwtService;

    /**
     * 登录
     */
    @PostMapping("/login")
    public Ret login(String mobile, String password){
        Assert.isBlank(mobile, "手机号不能为空");
        Assert.isBlank(password, "密码不能为空");

        //用户登录
        long userId = IUserService.login(mobile, password);

        //生成token
        String token = jwtService.generateToken(userId);

        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("expire", jwtService.getExpire());

        return Ret.ok(map);
    }

}
